package com.insight.modules.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import com.insight.common.api.vo.Result;
import com.insight.common.system.base.controller.JeecgController;
import com.insight.common.util.oConvertUtils;
import com.insight.modules.system.entity.SysGatewayRoute;
import com.insight.modules.system.service.ISysGatewayRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: gateway路由管理
 * @Author: jeecg-boot
 * @Date: 2020-05-26
 * @Version: V1.0
 */
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "gateway路由管理")
@RestController
@RequestMapping("/sys/gatewayRoute")
@Slf4j
public class SysGatewayRouteController extends JeecgController<SysGatewayRoute, ISysGatewayRouteService> {

	@Autowired
	private ISysGatewayRouteService sysGatewayRouteService;

	/**
	 * 批量更新所有路由配置
	 *
	 * @param json 路由配置的JSON对象，包含所有需要更新的路由数据
	 * @return 操作结果
	 */
	@ApiOperation(value = "批量更新所有路由配置", notes = "根据传入的JSON批量更新网关路由信息")
	@PostMapping(value = "/updateAll")
	public Result<?> updateAll(@ApiParam(value = "路由配置JSON", required = true) @RequestBody JSONObject json) {
		sysGatewayRouteService.updateAll(json);
		return Result.ok("操作成功！");
	}

	/**
	 * 查询所有路由列表
	 *
	 * @param sysGatewayRoute 作为查询条件的路由实体（目前无条件限制，可扩展）
	 * @return 路由列表JSONArray格式，predicates和filters字段转换为JSONArray对象
	 */
	@ApiOperation(value = "查询所有路由", notes = "查询所有网关路由，predicates和filters字段转换为JSON数组")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysGatewayRoute sysGatewayRoute) {
		LambdaQueryWrapper<SysGatewayRoute> query = new LambdaQueryWrapper<>();
		List<SysGatewayRoute> ls = sysGatewayRouteService.list(query);
		JSONArray array = new JSONArray();
		for (SysGatewayRoute rt : ls) {
			JSONObject obj = (JSONObject) JSONObject.toJSON(rt);
			if (oConvertUtils.isNotEmpty(rt.getPredicates())) {
				obj.put("predicates", JSONArray.parseArray(rt.getPredicates()));
			}
			if (oConvertUtils.isNotEmpty(rt.getFilters())) {
				obj.put("filters", JSONArray.parseArray(rt.getFilters()));
			}
			array.add(obj);
		}
		return Result.ok(array);
	}

	/**
	 * 清除Redis中的网关路由缓存
	 *
	 * @return 操作结果
	 */
	@ApiOperation(value = "清除Redis缓存", notes = "清除网关路由相关的Redis缓存")
	@GetMapping(value = "/clearRedis")
	public Result<?> clearRedis() {
		sysGatewayRouteService.clearRedis();
		return Result.ok("清除成功！");
	}

	/**
	 * 根据路由ID删除指定路由
	 *
	 * @param id 路由ID
	 * @return 删除结果
	 */
	//@RequiresRoles({"admin"})  // 这里可以根据需要加权限控制
	@ApiOperation(value = "删除指定路由", notes = "根据路由ID删除对应的网关路由")
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<?> delete(@ApiParam(value = "路由ID", required = true) @RequestParam(name = "id", required = true) String id) {
		sysGatewayRouteService.deleteById(id);
		return Result.ok("删除路由成功");
	}
}

